﻿layui.use(['layer', 'table', 'laypage', 'jquery'], function () {
    var $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer;

    var authorityButtonsListLogic = {
        //初始化
        init: function () {
            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                field: 'iSort',
                order: 'asc',
                listsUrl: "/Authority/Buttons/LoadListInfo",
                addUrl: "/Authority/Buttons/Add",
                modifyUrl: "/Authority/Buttons/Modify",
                removeUrl: "/Authority/Buttons/Remove"
            };
            this.Parameters.ac
                .pushQueue(function () {
                    CommonLogic.loadButtonsInfo(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    })
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();
        },
        //初始化列表
        initPage: function () {
            var title =
                [{ type: 'checkbox', fixed: 'left' },
                { field: 'gCode', title: 'Code', hide: true },
                {
                    field: 'Html', title: '按钮Html', width: 140, align: "center",
                    templet: function (d) {
                        return d.sHtml.replace("layui-btn", "layui-btn layui-btn-xs");
                    }
                },
                { field: 'sName', title: '按钮名称', width: 130, align: "center", sort: true },
                { field: 'sClass', title: '样式Class', width: 180, align: "center", sort: true },
                { field: 'sIcon', title: '样式Icon', width: 180, sort: true },
                { field: 'sId', title: '按钮ID', width: 220 },
                { field: 'iSort', title: '排序', width: 80, align: "center", sort: true },
                    { field: 'opt', title: '操作', toolbar: '#gridTool', width: '150', align: 'left' }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#buttonsList',
                url: self.Parameters.listsUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: $("#txtKeyWord").val(),
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "buttonsList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
        },
        //绑定事件
        bindEvent: function () {
            //监听(列表操作)
            table.on('tool(buttonsList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnEdit":
                        self.logicFunc.modifyInfo(data);
                        break;
                    case "funBtnRemove":
                        self.logicFunc.removeInfo(data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //新增
            $("#funBtnAdd").on("click", function () {
                self.logicFunc.addInfo();
            });

            //修改
            $("#funBtnEdit").on("click", function () {
                self.logicFunc.modifyInfo();
            });

            //删除
            $("#funBtnRemove").on("click", function () {
                self.logicFunc.removeInfo();
            });


        },
        //逻辑方法
        logicFunc: {

            ///获取数据
            loadListInfo: function () {
                GrdLayUI.tableReload({
                    domId: "buttonsList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: $("#txtKeyWord").val()
                });
            },

            //增加按钮信息
            addInfo: function () {
                var index = layui.layer.open({
                    title: "增加按钮信息",
                    type: 2,
                    resize: false,
                    area: ['500px', '450px'],
                    content: self.Parameters.addUrl,
                    end: function () {
                        $(window).unbind("resize");
                    }
                });
                //layui.layer.full(index);
            },

            //修改按钮信息
            modifyInfo: function (data) {
                if (data != undefined && data !=null) {
                    var index = layui.layer.open({
                        title: "修改按钮信息",
                        type: 2,
                        resize: false,
                        area: ['500px', '450px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "buttonsList",
                        backAction: function (data) {
                            var index = layui.layer.open({
                                title: "修改按钮信息",
                                type: 2,
                                resize: false,
                                area: ['600px', '450px'],
                                content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                        }
                    });
                }
            },

            //删除按钮信息
            removeInfo: function (data) {
                if (data != undefined && data != null) {
                    GrdLayUI.removeDataByInfo({
                        data: data,
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                } else {
                    GrdLayUI.removeData({
                        domId: "buttonsList",
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                }
            }
        },
        //工具
        tools: {

        }
    };
    authorityButtonsListLogic.init();
});
